<?php

App::uses('AppController', 'Controller');

class PagesController extends AppController {


	public $uses = array();

		function index()
    {
        $this->layout = "default";

				$this->loadModel('Notice');
				$Notices = $this->Notice->find("all", array("order"=>"id DESC"));
				$this->set(compact("Notices"));

				$this->loadModel('Config');
				$Config = $this->Config->findById(1);
				$this->set(compact("Config"));

		
				/*
				App::uses('CakeEmail', 'Network/Email');
				$Email = new CakeEmail();
				$Email->from(array('webmaster@ontimepro.com.br' => 'On Time Pro'));
				$Email->to('webmaster@ontimepro.com.br');
				$Email->subject('About');
				$Email->send('My message');
				*/

				if ($this->request->is('post'))
				{

					//die(debug($this->request->data));

					App::uses('CakeEmail', 'Network/Email');
					$Email = new CakeEmail();
					$Email->from(array('webmaster@ontimepro.com.br' => 'On Time Pro'));
					$Email->to('comercial@ontimepro.com.br');
					$Email->replyTo($this->request->data['Page']['email']);
					$Email->subject('Ontime Contato: '.$this->request->data['Page']['assunto']);
					$Email->send('Enviado por: '. $this->request->data['Page']['name'] . "\n \n".
					$this->request->data['Page']['mensagem']);


					$this->Session->setFlash("Enviado com sucesso!");

					// Trocar 2 por getInsertLastInsert
					$this->redirect(array('action'=>'index'));
				}
    }

		function about()
		{
				$this->layout = "default";
				$Page = $this->Page->findById(1);
				$this->set(compact("Page"));
				//die(debug($Page));
		}

		function carnaval()
		{
				$this->layout = "default";
				$Page = $this->Page->findById(3);
				$this->set(compact("Page"));
		}


		function admin_index()
		{
				$this->layout = "admin";
				$Pages = $this->Page->find("all");

				$this->set('Pages',$Pages);
		}
		function admin_add()
		{
				$this->layout = "admin";

				if ($this->request->is('post'))
				{
						//verifica se houve envio de imagem - avatar
						if(empty($this->request->data['Image'][0]['attachment']['name']))
							unset($this->request->data['Image'][0]);


						//die(debug($this->request->data));
						$this->Page->createWithAttachments($this->request->data);
						$this->Session->setFlash("Salvo com sucesso!");

						// Trocar 2 por getInsertLastInsert
						$this->redirect(array('action'=>'index'));
				}
		}
		function admin_edit($id=null)
		{
				$this->layout = "admin";

				if ($this->request->is(array('post', 'put')))
				{

						//verifica se houve envio de imagem - avatar
						if(empty($this->request->data['Image'][0]['attachment']['name']))
							unset($this->request->data['Image'][0]);

						//die(debug($this->request->data));
						$this->Page->createWithAttachments($this->request->data);
						$this->Session->setFlash(__('Salvo com sucesso!'), 'Flashs/FlashMessengeGood');
						$this->redirect(array('action'=>'edit',$id));

				}
				else
				{
						$this->data = $this->Page->find("first", array("recursive"=>2,"conditions"=>array("id"=>$id)));
				}
		}

		function admin_delete($id)
		{
				$this->Page->delete($id);
				$this->Session->setFlash('Removido com sucesso.', 'default', array(), 'good');
				$this->redirect('/admin/Pages');
		}
}
